# -*- coding: utf-8 -*-
# @Author   : zxc19
# @Time     : 2023/7/14 11:39
# @FileName : extract_util.py
# @Desc     : 请输入功能模块描述
import allure
from sqlalchemy import RowMapping

from plugins.sqlchemy_db import DBHelper, DBObj, DatabaseEnum
from plugins.sqlchemy_db.crud import execute

db_helper = DBHelper()
db_local_mysql = DBObj(
    sql_type=DatabaseEnum.MYSQL,
    host='127.0.0.1',
    port=3306,
    username='root',
    password='root',
    database='logaudit',
    use_ssh=True,
    ssh_host='10.121.110.40',
    ssh_port=22000,
    ssh_username='root',
    ssh_password='LSP@300768'
)


def extract_sql_execute(sql: str) -> RowMapping:
    """
    执行sql
    :param sql:
    :return:
    """
    conn_local_mysql = db_helper.get_connection(db_local_mysql)
    data = execute(conn_local_mysql.session, sql)
    return data[0][0]


class ExtractUtil(object):

    @staticmethod
    @allure.step('执行提取')
    def exe_extract(extracts):
        for extract in extracts:
            extract_way = extract.get('extract_way')
            if extract_way == 'sql':
                sql_str = extract.get('sql_str')
                extract_var = extract.get('extract_var')
                sql_res = extract_sql_execute(sql_str)
                if extract_var:
                    pass

